package menus;
import gui.CustomTable;
import gui.CustomTableModel;
import gui.ExceptionListener;
import gui.MainView;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import database.DriverData;


import java.sql.*;
import java.text.*;
public class DriverMenu extends MenuSuperclass implements ActionListener, ExceptionListener
{
    private DriverData driver = null;
    public DriverMenu(MainView mv)
    {
        this.mv = mv;
        driver = new DriverData();
        driver.addExceptionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
        String actionCommand = e.getActionCommand();
        if (actionCommand.equals("Insert Driver"))
        {
            DriverInsertDialog iDialog = new DriverInsertDialog(mv);
            iDialog.pack();
            mv.centerWindow(iDialog);
            iDialog.setVisible(true);
            return;
        }
        if (actionCommand.equals("Show Driver"))
        {
            showAllDrivers();
            return;
        }
    }
    private void showAllDrivers()
    {
        ResultSet rs = driver.showDriver();
        CustomTableModel model = new CustomTableModel(driver.getConnection(),
                                                      rs);
        CustomTable data = new CustomTable(model);
        model.addExceptionListener(this);
        data.addExceptionListener(this);
        mv.addTable(data);
    }
    class DriverInsertDialog extends JDialog implements ActionListener
    {
        private JTextField driverID = new JTextField(10);
        private JTextField driverName = new JTextField(15);
        private JTextField driverAddr = new JTextField(15);
        private JTextField driverCity = new JTextField(10);
        private JTextField driverBirth = new JTextField(10);
        private JTextField driverPhone = new JTextField(10);
        public DriverInsertDialog(JFrame parent)
        {
            super(parent, "Insert Driver", true);
            setResizable(false);
            JPanel contentPane = new JPanel(new BorderLayout());
            setContentPane(contentPane);
            contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10,
                                                                  10));
            JPanel inputPane = new JPanel();
            inputPane.setBorder(BorderFactory.createCompoundBorder(
                         new TitledBorder(new EtchedBorder(), "Driver Fields"),
                         new EmptyBorder(5, 5, 5, 5)));
            GridBagLayout gb = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
            inputPane.setLayout(gb);
            JLabel label= new JLabel("Driver SIN: ", SwingConstants.RIGHT);
            c.gridwidth = GridBagConstraints.RELATIVE;
            c.insets = new Insets(0, 0, 0, 5);
            c.anchor = GridBagConstraints.EAST;
            gb.setConstraints(label, c);
            inputPane.add(label);
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.insets = new Insets(0, 0, 0, 0);
            c.anchor = GridBagConstraints.WEST;
            gb.setConstraints(driverID, c);
            inputPane.add(driverID);
            label = new JLabel("Driver Name: ", SwingConstants.RIGHT);
            c.gridwidth = GridBagConstraints.RELATIVE;
            c.insets = new Insets(5, 0, 0, 5);
            c.anchor = GridBagConstraints.EAST;
            gb.setConstraints(label, c);
            inputPane.add(label);
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.insets = new Insets(5, 0, 0, 0);
            c.anchor = GridBagConstraints.WEST;
            gb.setConstraints(driverName, c);
            inputPane.add(driverName);
            label = new JLabel("Driver Address: ", SwingConstants.RIGHT);
            c.gridwidth = GridBagConstraints.RELATIVE;
            c.insets = new Insets(5, 0, 0, 5);
            c.anchor = GridBagConstraints.EAST;
            gb.setConstraints(label, c);
            inputPane.add(label);
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.insets = new Insets(5, 0, 0, 0);
            c.anchor = GridBagConstraints.WEST;
            gb.setConstraints(driverAddr, c);
            inputPane.add(driverAddr);
            label = new JLabel("Driver City: ", SwingConstants.RIGHT);
            c.gridwidth = GridBagConstraints.RELATIVE;
            c.insets = new Insets(5, 0, 0, 5);
            c.anchor = GridBagConstraints.EAST;
            gb.setConstraints(label, c);
            inputPane.add(label);
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.insets = new Insets(5, 0, 0, 0);
            c.anchor = GridBagConstraints.WEST;
            gb.setConstraints(driverCity, c);
            inputPane.add(driverCity);
            label = new JLabel("Driver Birthdate: ", SwingConstants.RIGHT);
            c.gridwidth = GridBagConstraints.RELATIVE;
            c.insets = new Insets(5, 0, 0, 5);
            c.anchor = GridBagConstraints.EAST;
            gb.setConstraints(label, c);
            inputPane.add(label);
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.insets = new Insets(5, 0, 0, 0);
            c.anchor = GridBagConstraints.WEST;
            gb.setConstraints(driverBirth, c);
            inputPane.add(driverBirth);
            label = new JLabel("Driver Phone: ", SwingConstants.RIGHT);
            c.gridwidth = GridBagConstraints.RELATIVE;
            c.insets = new Insets(5, 0, 0, 5);
            c.anchor = GridBagConstraints.EAST;
            gb.setConstraints(label, c);
            inputPane.add(label);
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.insets = new Insets(5, 0, 0, 0);
            c.anchor = GridBagConstraints.WEST;
            gb.setConstraints(driverPhone, c);
            inputPane.add(driverPhone);
            driverPhone.addActionListener(this);
            driverPhone.setActionCommand("OK");
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
            buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 2));
            JButton OKButton = new JButton("OK");
            JButton cancelButton = new JButton("Cancel");
            OKButton.addActionListener(this);
            OKButton.setActionCommand("OK");
            cancelButton.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        dispose();
                    }
                });
            buttonPane.add(Box.createHorizontalGlue());
            buttonPane.add(OKButton);
            buttonPane.add(Box.createRigidArea(new Dimension(10,0)));
            buttonPane.add(cancelButton);
            contentPane.add(inputPane, BorderLayout.CENTER);
            contentPane.add(buttonPane, BorderLayout.SOUTH);
            addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        dispose();
                    }
                });
        }
        public void actionPerformed(ActionEvent e)
        {
            String actionCommand = e.getActionCommand();
            if (actionCommand.equals("OK"))
            {
                if (validateInsert() != VALIDATIONERROR)
                {
                    dispose();
                }
                else
                {
                    Toolkit.getDefaultToolkit().beep();
                    JOptionPane errorPopup = new JOptionPane();
                    errorPopup.showMessageDialog(this, "Invalid Input",
                                    "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
        private int validateInsert()
        {
            try
            {
                Integer          did;
                String         dname;
                String         daddr;
                String         dcity;
                java.sql.Date dbirth;
                Integer       dphone;
                if (driverID.getText().trim().length() != 0)
                {
                    did = Integer.valueOf(driverID.getText().trim());
                    if (driver.findDriver(did.intValue()))
                    {
                        Toolkit.getDefaultToolkit().beep();
                        mv.updateStatusBar("Driver " + did.toString() +
                                            " already exists!");
                        return OPERATIONFAILED;
                    }
                }
                else
                {
                    return VALIDATIONERROR;
                }
                if (driverName.getText().trim().length() != 0)
                {
                    dname = driverName.getText().trim();
                }
                else
                {
                    return VALIDATIONERROR;
                }
                if (driverAddr.getText().trim().length() != 0)
                {
                    daddr = driverAddr.getText().trim();
                }
                else
                {
                    return VALIDATIONERROR;
                }
                if (driverCity.getText().trim().length() != 0)
                {
                    dcity = driverCity.getText().trim();
                }
                else
                {
                    return VALIDATIONERROR;
                }
                if (driverBirth.getText().trim().length() != 0)
                {
                    String stringDate = driverBirth.getText().trim();
                    SimpleDateFormat fm = new SimpleDateFormat("dd/MM/yy");
                    java.util.Date utilDate = fm.parse(stringDate);
                    dbirth = new java.sql.Date(utilDate.getTime());
                }
                else
                {
                    return VALIDATIONERROR;
                }
                if (driverPhone.getText().trim().length() != 0)
                {
                    dphone = Integer.valueOf(driverPhone.getText().trim());
                }
                else
                {
                    dphone = null;

                }
                mv.updateStatusBar("Inserting driver...");
                if (driver.insertDriver(did, dname, daddr, dcity, dbirth,
                    dphone))
                {
                    mv.updateStatusBar("Operation successful.");
                    showAllDrivers();
                    return OPERATIONSUCCESS;
                }
                else
                {
                    Toolkit.getDefaultToolkit().beep();
                    mv.updateStatusBar("Operation failed.");
                    return OPERATIONFAILED;
                }
            }
            catch (NumberFormatException ex)
            {
                return VALIDATIONERROR;
            }
            catch (ParseException ex)
            {
                return VALIDATIONERROR;
            }
        }
    }
}
